/**
 * created by 小卷毛, 2020/9/14
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.lib_tools_database.factory;

import android.text.TextUtils;

import com.sgcc.pda.lib_tools_database.bean.other.AppNoForPlan;
import com.sgcc.pda.lib_tools_database.bean.other.TicketState;
import com.sgcc.pda.lib_tools_database.bean.other.TicketStatePlan;
import com.sgcc.pda.lib_tools_database.bean.other.TicketStateSign;
import com.sgcc.pda.lib_tools_database.gen.AppNoForPlanDao;
import com.sgcc.pda.lib_tools_database.gen.DaoSession;
import com.sgcc.pda.lib_tools_database.gen.TicketStateDao;
import com.sgcc.pda.lib_tools_database.gen.TicketStateSignDao;

import org.greenrobot.greendao.Property;

import java.util.List;

/**
 * 描 述：工作票状态表操作类
 * 作 者：小卷毛  2020-09-14 20:05
 * 修改描述：增加参数字段
 * 修 改 人：
 * 修改版本：v1.2.1
 */
public class TicketStateImpl extends Abstractable<TicketState, TicketStateDao> {

    @Override
    public TicketStateDao getDao(DaoSession session) {
        return session.getTicketStateDao();
    }

    /**
     * 根据工作计划编号获取工作票状态
     */
    public String findTicketStateByplanNo(String planNo) {
        TicketStatePlan ticketPlan = findTicketPlanByPlanNo(planNo);
        if (ticketPlan != null) {
            TicketState ticketDetail = ticketPlan.getWtDetail();
            if (ticketDetail != null) {
                if (!TextUtils.isEmpty(ticketDetail.getWtState())) {
                    return ticketDetail.getWtState();
                }
            }
        }
        return "";
    }

    /**
     * 根据工作票号查询工作票状态
     */
    public TicketState findTicketByWtNo(String wtNo) {
        TicketState ticket = findData(TicketStateDao.Properties.WtNo, wtNo);

        if (ticket != null) {
            List<TicketStateSign> list = new TicketStateSignImpl().findByParam(
                    new Property[]{TicketStateSignDao.Properties.WtNo},
                    new String[]{ticket.getWtNo()}
            );

            ticket.setTicketSignInfo(list);
            return ticket;
        } else return null;
    }

    /**
     * 根据工单号获取工作票状态
     */
    public String findTicketStateByAppNo(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo
    ) {
        // 获取工作计划和工单关系表
        AppNoForPlan planData = new AppNoForPlanImpl()
                .getDataByAppNo(
                        appNo,
                        appCode,
                        eomOrderSrc,
                        workTypeNo);

        // 获取工作票状态详情
        TicketStatePlan ticketPlan =
                findTicketPlanByPlanNo(planData.getPlanNo());

        if (ticketPlan != null) {
            TicketState ticketDetail = ticketPlan.getWtDetail();
            if (ticketDetail != null) {
                if (!TextUtils.isEmpty(ticketDetail.getWtState())) {
                    return ticketDetail.getWtState();
                }
            }
        }
        return "";
    }

    /**
     * 根据工单号获取工作票号
     */
    public String findWtNoByAppNo(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo
    ) {
        if (TextUtils.isEmpty(appNo)) return "";

        // 获取工作计划和工单关系表
        AppNoForPlan item = new AppNoForPlanImpl()
                .getDataByAppNo(
                        appNo,
                        appCode,
                        eomOrderSrc,
                        workTypeNo);

        if (item != null) {
            if (!TextUtils.isEmpty(item.getWtNo())) {
                return item.getWtNo();
            }
        }
        return "";
    }

    /**
     * 根据工单号获取工作票信息（工作计划）
     */
    public TicketStatePlan findTicketPlanByAppNo(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo
    ) {
        if (TextUtils.isEmpty(appNo)) return null;

        // 获取工作计划和工单关系表
        AppNoForPlan item = new AppNoForPlanImpl()
                .getDataByAppNo(
                        appNo,
                        appCode,
                        eomOrderSrc,
                        workTypeNo);

        if (item != null) {
            TicketStatePlan data = new TicketStatePlan();
            data.setPlanNo(item.getPlanNo());
            data.setWtExist(item.getWtExist());
            data.setMeetingRequire(item.getMeetingRequire());
            data.setRiskRequire(item.getRiskRequire());

            // 查询工作票状态
            TicketState ticket = new TicketStateImpl()
                    .findData(TicketStateDao.Properties.PlanNo, item.getPlanNo());

            if (ticket != null) {
                List<TicketStateSign> list = new TicketStateSignImpl().findByParam(
                        new Property[]{TicketStateSignDao.Properties.WtNo},
                        new String[]{ticket.getWtNo()}
                );

                ticket.setTicketSignInfo(list);
                data.setWtDetail(ticket);
            }

            return data;
        } else {
            return null;
        }
    }

    /**
     * 根据工作计划编号查询工作票状态
     */
    public TicketStatePlan findTicketPlanByPlanNo(String planNo) {
        if (TextUtils.isEmpty(planNo)) return null;

        // 获取工作计划和工单关系表
        AppNoForPlan item = new AppNoForPlanImpl()
                .findData(AppNoForPlanDao.Properties.PlanNo, planNo);

        if (item != null) {
            TicketStatePlan data = new TicketStatePlan();
            data.setPlanNo(planNo);
            data.setWtExist(item.getWtExist());
            data.setMeetingRequire(item.getMeetingRequire());
            data.setRiskRequire(item.getRiskRequire());

            // 查询工作票状态
            TicketState ticket = new TicketStateImpl()
                    .findData(TicketStateDao.Properties.PlanNo, planNo);

            if (ticket != null) {
                List<TicketStateSign> list = new TicketStateSignImpl().findByParam(
                        new Property[]{TicketStateSignDao.Properties.WtNo},
                        new String[]{ticket.getWtNo()}
                );

                ticket.setTicketSignInfo(list);
                data.setWtDetail(ticket);
            }

            return data;
        } else
            return null;
    }

    /**
     * 根据工单号、工作计划保存工作票状态
     */
    public void insertData(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            TicketStatePlan bean
    ) {
        if (bean != null && bean.getWtDetail() != null) {
            // 删除关系表数据
            new AppNoForPlanImpl()
                    .deleteByAppNo(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo);
            // 插入新关系表数据
            new AppNoForPlanImpl()
                    .insertData(
                            appNo,
                            bean.getPlanNo(),
                            appCode,
                            eomOrderSrc,
                            workTypeNo,
                            bean.getWtDetail().getWtNo(),
                            bean.getWtExist(),
                            bean.getMeetingRequire(),
                            bean.getRiskRequire()
                    );

            // 赋值变量
            TicketState detail = bean.getWtDetail();
            detail.setPlanNo(bean.getPlanNo());
            detail.setAppNo(appNo);

            // 查询工作票签发列表
            List<TicketStateSign> list = new TicketStateSignImpl().findByParam(
                    new Property[]{TicketStateSignDao.Properties.WtNo},
                    new String[]{detail.getWtNo()}
            );

            // 删除工作票签发列表
            if (list != null && list.size() > 0) {
                for (TicketStateSign item : list) {
                    new TicketStateSignImpl().delete(item);
                }
            }

            // 插入新的工作票签发列表
            if (detail.getTicketSignInfo() != null) {
                for (TicketStateSign item : detail.getTicketSignInfo())
                    item.setWtNo(detail.getWtNo());
                new TicketStateSignImpl().insertList(detail.getTicketSignInfo());
            }

            // 插入工作票详情
            insert(detail);
        }
    }

}
